//
//  CNBBSChannelItem.m
//  CsdnPlus
//
//  Created by 孟瑞瑞 on 2018/12/26.
//  Copyright © 2018 CSDN. All rights reserved.
//

#import "CNBBSChannelItem.h"
#import <CNBaseTool/CNCategoryHeader.h>
@interface CNBBSChannelItem ()
{
    UILabel *_textLabel;
    
    CAShapeLayer *_borderLayer;
    
}
@end

@implementation CNBBSChannelItem

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}

-(void)initUI
{
    self.userInteractionEnabled = true;
    //self.layer.cornerRadius = 10.0f;
    //    self.layer.borderColor = categoryBordColor.CGColor;
    //    self.layer.borderWidth = 0.5f;
    self.theme_backgroundColor = pageBGColorPicker;
    
    _textLabel = [[UILabel alloc]init];
    _textLabel.frame = self.bounds;
    _textLabel.textAlignment = NSTextAlignmentCenter;
    _textLabel.theme_textColor = channeBBSBGColorPicker;
    _textLabel.adjustsFontSizeToFitWidth = true;
    _textLabel.userInteractionEnabled = true;
    _textLabel.layer.cornerRadius = 10.0f;
    _textLabel.layer.theme_borderColor = categoryBordCGColorPicker;
    _textLabel.layer.borderWidth = 0.5f;
    _textLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:_textLabel];
    
    self.imageBgView = [[UIView alloc]initWithFrame:CGRectMake(self.bounds.size.width - 16, 0, 16, 16)];
    _imageBgView.theme_backgroundColor = pageBGColorPicker;
    [_imageBgView bringSubviewToFront:self];
    [self addSubview:_imageBgView];
    self.imageView = [[UIImageView alloc]init];
    //_imageView.frame = CGRectMake(self.bounds.size.width - 16, 0, 16, 16);
    [_imageBgView addSubview:self.imageView];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(16, 16));
    }];
    
    //_imageView.image = [UIImage imageNamed:@"delete"];
    
    [self addBorderLayer];
}

-(void)addBorderLayer{
    _borderLayer = [CAShapeLayer layer];
    _borderLayer.bounds = self.bounds;
    _borderLayer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    _borderLayer.path = [UIBezierPath bezierPathWithRoundedRect:_borderLayer.bounds cornerRadius:_textLabel.layer.cornerRadius].CGPath;
    _borderLayer.lineWidth = 0.5f;
    _borderLayer.lineDashPattern = @[@5, @3];
    _borderLayer.fillColor = [UIColor clearColor].CGColor;
    _borderLayer.theme_strokeColor = categoryBordCGColorPicker;
    [_textLabel.layer addSublayer:_borderLayer];
    _borderLayer.hidden = true;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    _textLabel.frame = self.bounds;
}

#pragma mark -
#pragma mark 配置方法

#pragma mark -
#pragma mark Setter

-(void)setTitle:(NSString *)title
{
    _title = title;
    _textLabel.text = title;
}

-(void)setIsMoving:(BOOL)isMoving
{
    _isMoving = isMoving;
    if (_isMoving) {
        self.backgroundColor = [UIColor clearColor];
        _borderLayer.hidden = false;
    }else{
        self.theme_backgroundColor = pageBGColorPicker;
        _borderLayer.hidden = true;
    }
}

-(void)setIsFixed:(BOOL)isFixed{
    _isFixed = isFixed;
    if (isFixed) {
        _textLabel.theme_textColor = channeBBSBGColorPicker;
        
    }else{
        _textLabel.theme_textColor = cellTitleTextColorPicker;
    }
}
@end
